import java.util.*;
public class Text {
    public static void main(String[] args) {
        /*Map<Integer, Integer> hashmap = new HashMap<>();
        int[] array = {1,2,3,3,5,6,6,7,1,2};
        for (int i = 0; i < array.length; i++) {
            hashmap.put(array[i],hashmap.getOrDefault(array[i],0)+1);
        }
        for (Map.Entry<Integer,Integer> entry : hashmap.entrySet()) {
            if (entry.getValue() >= 2){
                System.out.println("key "+entry.getKey()+" "+"value "+entry.getValue());
            }
        }
        PriorityQueue<Map.Entry<String,Integer>> priorityQueue = new PriorityQueue<>(
                new Comparator<Map.Entry<String,Integer>>(){
            public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2){
                return o1.getValue().compareTo(o2.getValue());
            }
        });*/
    }
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> hashMap = new HashMap<>();
        for(int i = 0; i < nums.length; i++) {
            if(!hashMap.containsKey(nums[i])) {
                hashMap.put(nums[i],i);
            }else{
                if(i - hashMap.get(nums[i])  <= k) {
                    return true;
                }
                hashMap.put(nums[i],i);
            }
        }
        return false;
    }
    public List<String> topKFrequent(String[] words, int k) {
        PriorityQueue<Map.Entry<String,Integer>> priorityQueue = new PriorityQueue<>(k,
                new Comparator<Map.Entry<String,Integer>>(){
                    public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2){
                        if(o1.getValue().compareTo(o2.getValue()) == 0){
                            return o2.getKey().compareTo(o1.getKey());
                        }
                        return o1.getValue().compareTo(o2.getValue());
                    }
                });
        Map<String,Integer> hashMap = new HashMap<>();
        for(int i = 0; i < words.length; i++) {
            if(!hashMap.containsKey(words[i])) {
                hashMap.put(words[i],1);
            }else{
                hashMap.put(words[i],hashMap.get(words[i])+1);
            }
        }

        for(Map.Entry<String,Integer> entry:hashMap.entrySet()) {
            if(priorityQueue.size() < k){
                priorityQueue.offer(entry);
            }else{
                if(priorityQueue.peek().getValue().compareTo(entry.getValue()) < 0) {
                    priorityQueue.poll();
                    priorityQueue.offer(entry);
                }else{
                    if(priorityQueue.peek().getValue().compareTo(entry.getValue()) == 0) {
                        if(priorityQueue.peek().getKey().compareTo(entry.getKey()) > 0) {
                            priorityQueue.poll();
                            priorityQueue.offer(entry);
                        }
                    }
                }
            }
        }
        List<String> list = new ArrayList<>();
        while(!priorityQueue.isEmpty()) {
            list.add(priorityQueue.poll().getKey());
        }
        Collections.reverse(list);
        return list;
    }
}
